
/**
** created by zhouzhineng 15-07-01
** 主要用于编译android 变色龙v2的属性配置文件
**/

var path  = require('path'),
	fs = require('fs');

var g_proBuffer = null;
var g_filePath = null;

/**
** 设置属性
** key 属性名
** value 属性值
**/
module.exports.setProperty = function(key,value) {
	var pos = getVaildPropertySettingLinePos(g_proBuffer,key);
	if(pos != -1 ){
        var prev = g_proBuffer.substring(0,pos-1);
        var back = g_proBuffer.substring(pos,g_proBuffer.length - 1);
        var replace = back.substring(back.indexOf('=') + 1 , back.length);
        g_proBuffer = prev + back.replace(replace,value);

	} else {
		g_proBuffer +=  ("\n"+ key + "=" + value);
	}
}

/**
**  获取指定属性的值
**  key 属性名
**/
module.exports.getProperty = function(key) {
	if(checkPropertyExist(key)) {
		var propertyLine = getPropertySettingLine(key);
		var idxStart = propertyLine.indexOf('=');
		var idxEnd = propertyLine.indexOf('\n');
		if(idxStart < 0 || idxEnd < 0) {
			return null;
		}
		var propertyVal = propertyLine.substring(idxStart + 1, idxEnd);
		return propertyVal;	
	}

	return null;
}

/**
** 初始化
**/
module.exports.init = function(filePath) {
	g_filePath = null;
	g_proBuffer = null;
	g_filePath = filePath;
	g_proBuffer = fs.readFileSync(filePath,{encoding:'utf-8'});
}

/**
**  反初始化
**/
module.exports.uninit = function() {
	g_filePath = null;
	g_proBuffer = null;
}

/**
** 保存设置
**/
module.exports.save = function() {
	if(g_filePath != null && g_proBuffer != null) {
		fs.writeFileSync(g_filePath, g_proBuffer, {encoding:'utf-8'});
		return true;
	} else {
		return false;
	}
}

/**
**   检查指定属性是否存在
**   key 属性名
**/
function checkPropertyExist(key) {
	var pos = getVaildPropertySettingLinePos(g_proBuffer,key);
	if(pos > 0) {
		return true;
	}
	return false;
}

/**
** 获取指定属性的设置行
** key 属性名
**/
function getPropertySettingLine(key) {
		var pos = getVaildPropertySettingLinePos(g_proBuffer,key);
		if(pos < 0) {
			return null;
		}
		var operateString = g_proBuffer.substring(pos,g_proBuffer.length - 1 );
		var idxEnd = operateString.indexOf('\n');
		var property = operateString.substring(0,idxEnd - 1 );
		return property;
}

/**
**  获取指定key设置的有效位置
**
**/
function getVaildPropertySettingLinePos(proBuffer,key) {
	var tmpBuffer = proBuffer;
	var hasTarget = false;
	if(tmpBuffer != null && key != null) {
		var pos = -1;
		var properties = tmpBuffer.split('\n');
		for(var i = 0; i < properties.length; i++) {
			if(properties[i].indexOf(key) >=0 && properties[i].indexOf('#') < 0) {
				hasTarget = true;
				return pos;
			}else {
				pos += properties[i].length + 1;
			}
		}
		if(!hasTarget) {
			return -1;
		}
	}
	return -1;
}

/**
**使配置行有效或无效
** key 配置名
** enable 使能标志
**/
function enablePropertySettingLine(key,enable) {

}


/**
** 擦除指定属性的设置行
** key 属性名
**/
function erasePropertySettingLine(key) {
	var pos = getVaildPropertySettingLinePos(g_proBuffer,key);
	var operateString = g_proBuffer.substring(pos,g_proBuffer.length - 1);
	var idxEnd = operateString.indexOf('\n');
	operateString = operateString.substring(pos,idxEnd);
	g_proBuffer = g_proBuffer.substring(0,pos - 1) + operateString;
}



